import 'package:flutter_test/flutter_test.dart';
import 'package:music_example/services/services.dart';

void main() {
  group('AudioPlayerTool Utility Tests', () {
    test('应该正确格式化时长', () {
      const duration = Duration(minutes: 3, seconds: 45);
      final formatted = AudioPlayerTool.formatDuration(duration);
      expect(formatted, equals('03:45'));
    });

    test('应该正确格式化零时长', () {
      const duration = Duration.zero;
      final formatted = AudioPlayerTool.formatDuration(duration);
      expect(formatted, equals('00:00'));
    });

    test('应该正确格式化长时长', () {
      const duration = Duration(hours: 1, minutes: 23, seconds: 45);
      final formatted = AudioPlayerTool.formatDuration(duration);
      expect(formatted, equals('83:45')); // 83 分钟 45 秒
    });
  });

  group('AudioException Tests', () {
    test('AudioException 应该正确创建', () {
      const exception = AudioPlayException('测试错误');
      expect(exception.message, equals('测试错误'));
      expect(exception.code, isNull);
      expect(exception.toString(), equals('AudioException: 测试错误'));
    });

    test('AudioException 带代码应该正确创建', () {
      const exception = AudioPlayException('测试错误', code: 'TEST_ERROR');
      expect(exception.message, equals('测试错误'));
      expect(exception.code, equals('TEST_ERROR'));
      expect(exception.toString(), equals('AudioException(TEST_ERROR): 测试错误'));
    });

    test('AudioPlayException 应该正确创建', () {
      const exception = AudioPlayException('播放错误', code: 'PLAY_ERROR');
      expect(exception.message, equals('播放错误'));
      expect(exception.code, equals('PLAY_ERROR'));
      expect(exception, isA<AudioException>());
    });

    test('AudioLoadException 应该正确创建', () {
      const exception = AudioLoadException('加载错误', code: 'LOAD_ERROR');
      expect(exception.message, equals('加载错误'));
      expect(exception.code, equals('LOAD_ERROR'));
      expect(exception, isA<AudioException>());
    });

    test('AudioNetworkException 应该正确创建', () {
      const exception = AudioNetworkException('网络错误', code: 'NETWORK_ERROR');
      expect(exception.message, equals('网络错误'));
      expect(exception.code, equals('NETWORK_ERROR'));
      expect(exception, isA<AudioException>());
    });

    test('AudioPermissionException 应该正确创建', () {
      const exception =
          AudioPermissionException('权限错误', code: 'PERMISSION_ERROR');
      expect(exception.message, equals('权限错误'));
      expect(exception.code, equals('PERMISSION_ERROR'));
      expect(exception, isA<AudioException>());
    });
  });

  group('AudioStateListener Tests', () {
    late AudioStateListener listener;

    setUp(() {
      listener = AudioStateListener();
    });

    tearDown(() {
      listener.dispose();
    });

    test('应该正确创建监听器', () {
      expect(listener.isListening, isFalse);
    });

    // 跳过需要平台通道的测试
    test('应该正确释放资源', () {
      // 测试不启动监听的情况下释放资源
      expect(listener.isListening, isFalse);
      listener.dispose();
      expect(listener.isListening, isFalse);
    });
  });
}
